package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * @author: JiangXFei
 * @date: 2017/11/23 0023
 * @content:果蔬商品类别选择
 */

public class FreshFruitsCassifySelectActivity extends BaseActivity
        implements FreshConstant.GetFreshCassifyView {
    @InjectView(R.id.recy_type_cassify)
    RecyclerView recyTypeCassify;
    @InjectView(R.id.recy_type_item)
    RecyclerView recyTypeItem;
    private List<FreshFruitCassifyBean> cassifyTypeBeans = new ArrayList<>();
    private List<FreshFruitCassifyBean> cassifyItemBeans = new ArrayList<>();
    private MenuAdapter menuTypeAdapter;
    private MenuAdapter menuItemAdapter;
    private int typeId = 0;
    private FreshConstant.GetFreshCassifyPersenter getFreshCassifyPersenter;
    private Intent intent;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fresh_fruits_cassify_selelct_layout);
        ButterKnife.inject(this);
        initTitle();
        mContext = this;
        mActivity = this;
        getFreshCassifyPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshCassifyView) mActivity);
        initMainRecy();
        initSecentRecy();
        getFreshCassifyPersenter.getFreshCassify();
        intent = getIntent();

    }

    /**
     * 初始化小类别适配器
     */
    private void initSecentRecy() {
        menuItemAdapter = new MenuAdapter(R.layout.item_flea_menu_classify, cassifyItemBeans);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyTypeItem.setLayoutManager(linearLayoutManager);
        menuItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent.putExtra(StringConstants.FLEATYPE, cassifyItemBeans.get(position).getName());
                intent.putExtra(StringConstants.FLEATYPEID, Integer.valueOf(cassifyItemBeans.get(position).getId()));
                mActivity.setResult(IntConstants.FRESHSELECTCASSIFYID, intent);
                mActivity.finish();
            }
        });
        recyTypeItem.setAdapter(menuItemAdapter);
    }

    /**
     * 初始化分类适配器
     */
    private void initMainRecy() {
        menuTypeAdapter = new MenuAdapter(R.layout.item_flea_menu_classify, cassifyTypeBeans);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyTypeCassify.setLayoutManager(linearLayoutManager);
        menuTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < cassifyTypeBeans.size(); i++) {
                    cassifyTypeBeans.get(i).setChecked(false);
                }
                cassifyTypeBeans.get(position).setChecked(true);
                menuTypeAdapter.notifyDataSetChanged();
                typeId = Integer.valueOf(cassifyTypeBeans.get(position).getId());
                getFreshCassifyPersenter.getFreshCassify();
            }
        });
        recyTypeCassify.setAdapter(menuTypeAdapter);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("宝贝分类");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getFreshCassifyId() {
        return typeId;
    }

    @Override
    public void getFreshCassifyOnSuccess(List<FreshFruitCassifyBean> cassifyBeans) {
        if (cassifyTypeBeans.size() == 0) {
            cassifyTypeBeans.addAll(cassifyBeans);
            cassifyTypeBeans.get(0).setChecked(true);
            menuTypeAdapter.notifyDataSetChanged();
            typeId = Integer.valueOf(cassifyTypeBeans.get(0).getId());
            getFreshCassifyPersenter.getFreshCassify();
        } else {
            cassifyItemBeans.clear();
            cassifyItemBeans.addAll(cassifyBeans);
            menuItemAdapter.notifyDataSetChanged();
        }
    }

    private class MenuAdapter extends BaseQuickAdapter<FreshFruitCassifyBean, BaseViewHolder> {

        public MenuAdapter(int layoutResId, @Nullable List<FreshFruitCassifyBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FreshFruitCassifyBean item) {
            helper.setText(R.id.tv_menu_one, item.getName());
            if (item.isChecked()) {
                helper.setTextColor(R.id.tv_menu_one, Color.parseColor("#66caff"));
            } else {
                helper.setTextColor(R.id.tv_menu_one, Color.parseColor("#707070"));
            }
        }
    }
}
